/**
 * Created by dwj on 2017/2/28.
 */
Rsd.define('Rsd.data.Menu', {
    extend: 'Rsd.common.Object',
    xtype: 'menu',
    text: 'unnamed',
    icon: null,
    /*页面*/
    viewType: '',
    key: '',
    //提示信息
    tip: '',
    parent: null,
    /*
    * 子节点
    * */
    //children:[],
    tag: null,
    /*
    * 页面参数:config
    * */
    args: null,
    /**
     * 
     */
    onAfterAddItem: null,
    /*
    *
    * */
    constructor: function Menu(config) {

        config = config || {};
        
        this.apply(config);
        this.__mapping = {}; 
        
        var me = this;
        if (!Rsd.isEmpty(me.path)) {
            
            var _plugin = me; 
            var _fn = function () {
                var _p = this;
                var _s = new Rsd.data.Store({
                    proxy: {
                        url: _plugin.path + '/config.js',
                        method: 'get'
                    }
                });
 
                _s.load({}, function (pConfig) {
                   
                    if (pConfig) {
                        //加载子菜单
                        me.loadData(pConfig.children);
                        //加载插件
                        pConfig.path = _p.path;
                        Rsd.app.loadPlugin(pConfig); 
                    }
                });
            }

            _fn.call(_plugin);
        }
    },
    /**
     * @description 加载菜单数据，加载前会清除所有子菜单
     */
    loadData: function loadData(data) {
        var _ds = data || [];
        if (Rsd.isArray(_ds)) {
            this.children = _ds;
        } 
    },
    /**
    *  @description 清除所有子菜单
    * */
    removeAll: function removeAll() {
        this.__mapping = {};
        this.children = [];
    },
    /**
    * @description 添加子菜单
    *  */
    addItem: function addItem(menu) {
        //debugger;
        var me = this;
        var _child = menu;
        if (_child == null || _child == undefined) {
            return;
        }
        if (_child instanceof Array) {
            for (var i in _child) {
                this.addItem(_child[i]);
            }

            return _child;
        }
  
        if (_child instanceof Rsd.data.Menu) {
            //
        } else {
            _child = Rsd.create('Rsd.data.Menu', Rsd.isObject(menu) ? menu : { text: menu }); 
        }
        if (_child.key == null || _child.key == undefined || _child.key == '') {
            _child.key = me.key + '_' + me.children.length;
        }
        if (_child.onAfterAddItem == null || _child.onAfterAddItem == undefined) {
            _child.onAfterAddItem = me.onAfterAddItem;
        }

        _child.parent = me;

        this.__mapping[_child.key] = _child;
        me.children.push(_child);
        setTimeout(function () {
            me.funApplyByIOC(me.onAfterAddItem, [_child]);
        }, 0);
  
        return _child;
    },
    /** 
     * @description 获取子菜单
     * */
    getItem: function getItem(key) {
        if (Rsd.isEmpty(key)) {
            throw new Error('argument key is not allow null.');
        }
        return this.__mapping[key];
    }

}, function (type) {

    var childrenGetter = function () {
        //debugger;
        if (this.__children == undefined) {
            this.__children = [];
        }

        return this.__children;
    };
    var childrenSetter = function (children) {

        if (this.__children == undefined) {
            this.__children = [];
        }

        if (Rsd.isArray(children)) {
            this.__mapping = {};
            this.__children = [];
            for (var i in children) {
                this.addItem(children[i]);
            }
        }
    }

    this.defineProperty(type, "children", childrenGetter, childrenSetter, true);
});